package com.ecoolex.chinadutyfree.retailer.core.manager;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecoolex.chinadutyfree.retailer.core.dao.CouponVerifyingConsumptionDAO;
import com.ecoolex.chinadutyfree.retailer.entity.Coupon;
import com.ecoolex.chinadutyfree.retailer.entity.CouponVerifyingConsumption;
import com.ecoolex.chinadutyfree.retailer.entity.CouponVerifyingConsumptionExample;
import com.ecoolex.chinadutyfree.retailer.entity.CouponVerifyingConsumptionExample.Criteria;

@Service
public class CouponVerifyingConsumptionManager {
	
    @Autowired
    private CouponVerifyingConsumptionDAO dao;
    
    public void create(Coupon coupon,String orderNo,BigDecimal realAmt,String createUser) {
    	CouponVerifyingConsumption record = new CouponVerifyingConsumption();
    	record.setCouponId(coupon.getCouponId());
    	record.setCouponClassId(coupon.getCouponClassId());
    	record.setUserId(coupon.getUserId());
    	record.setOrderNo(orderNo);
    	record.setRealPrice(realAmt);
    	record.setDeleted(CouponVerifyingConsumption.NOT_DELETED);
    	record.setCreateUser(createUser);
    	record.setCreateTime(Date.from(Instant.now()));
    	record.setUpdateUser(createUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	dao.insert(record);
    }
    
    public CouponVerifyingConsumption getCouponVerifyingConsumptionByCouponId(Integer couponId) {
		CouponVerifyingConsumptionExample example = new CouponVerifyingConsumptionExample();
		Criteria criteria = example.createCriteria();
		criteria.andCouponIdEqualTo(couponId).andDeletedEqualTo(CouponVerifyingConsumption.NOT_DELETED);
		
    	return dao.selectOneByExample(example);
	}
}